<?php

class Evento {
	function __construct() {}

	function insert( $evento) {
		preg_match_all('/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/', $evento['fecha_inicio'], $matches);
		$fini = $matches[3][0]."-".$matches[2][0]."-".$matches[1][0]." ".$evento['hora_inicio'].":00";

		preg_match_all('/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/', $evento['fecha_fin'], $matches);
		$ffin = $matches[3][0]."-".$matches[2][0]."-".$matches[1][0]." ".$evento['hora_fin'].":00";
	

		$chk_hora = $evento['chk_hora'];
		$chk_fini = $evento['chk_fecha_inicio'];
		$chk_ffin = $evento['chk_fecha_fin'];

		switch( $evento['recurrente']) {
			case "no":
				$rec = 0;
				$cada = 0;
				break;
			case "diaria":
				$rec = 1;
				$cada = $evento['cada_dias'];
				break;
			case "semanal":
				$rec = 2;
				$cada = $evento['cada_semanas'];
				break;
			case "mensual":
				$rec = 3;
				$cada = $evento['cada_meses'];
				break;
			case "anual":
				$rec = 4;
				$cada = $evente['cada_anos'];
				break;
		}

		$tipo = $evento['tipo'] == "Evento" ? 0 : 1;

		$query = "INSERT INTO `Evento`(`Cal_id`, `Even_titulo`, `Even_ubicacion`, `Even_fini`, `Even_ffin`, `Even_descrip`, `Even_categoria`, `Even_ambito`, `Even_prioridad`, `Even_tipo`, `Even_tiene_fini`, `Even_tiene_ffin`, `Even_tiene_hor`, `Even_recurrente`, `Even_cada`, `Even_lu`, `Even_ma`, `Even_mi`, `Even_ju`, `Even_vi`, `Even_sa`, `Even_do`) VALUES ";
		$query .= sprintf( "( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');", $evento['cal_id'], $evento['titulo'], $evento['ubicacion'], $fini, $ffin, $evento['descripcion'], $evento['categoria'], $evento['ambito'], $evento['prioridad'], $tipo, $chk_fini, $chk_ffin, $chk_hora, $rec, $cada, $evento['chk_lu'], $evento['chk_ma'], $evento['chk_mi'], $evento['chk_ju'], $evento['chk_vi'], $evento['chk_sa'], $evento['chk_do']);
		if( ! $GLOBALS['mysql_connection']->query( $query)) return false;
		return true;
	}

	function get_eventos( $sem, $usu_id) {
		$query = "SELECT * FROM Calendario NATURAL LEFT JOIN Evento WHERE Calendario.Op_id = '$usu_id' AND Even_fini >= '$sem[0]' AND Even_ffin <= '$sem[1]';";
		if( !$result = $GLOBALS['mysql_connection']->query( $query)) return NULL;
		
		if( $result->num_rows == 0) return array();

		$eventos = array();
		while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
			$eventos[] = $row;
		}
		return $eventos;
	}

	function get_tareas( $usu_id) {
		date_default_timezone_set('America/Santiago');
		$time = time();
		$fech = date('Y-m-d H:i:s', $time);

		$query = "SELECT * FROM Calendario NATURAL LEFT JOIN Evento WHERE Calendario.Op_id = '$usu_id' AND Even_ffin >= '$fech' AND Even_tipo = '1' ORDER BY Even_ffin ASC;";
		if( !$result = $GLOBALS['mysql_connection']->query( $query)) return NULL;
		
		$tareas = NULL;
		if( $result->num_rows == 0) return NULL;

// 		$tareas = array();
		while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
			$tareas[] = $row;
		}

		$query = "SELECT * FROM Calendario NATURAL LEFT JOIN Evento WHERE Calendario.Op_id = '$usu_id' AND Even_ffin < '$fech' AND Even_tipo = '1' ORDER BY Even_ffin ASC;";
		if( !$result = $GLOBALS['mysql_connection']->query( $query)) return NULL;
		
		if( $result->num_rows == 0) return $tareas;

// 		$tareas = array();
		while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
			$tareas[] = $row;
		}
		return $tareas;
	}
}
 
